/*
 * @Copyright: 2017 www.yyfax.com Inc. All rights reserved.
 */
package com.yyfax.pay.order.model.to.pay;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.NotBlank;

import com.yyfax.commons.lang.DateUtil;
import com.yyfax.commons.lang.IdCardValidateUtil;
import com.yyfax.commons.lang.ValidateUtil;
import com.yyfax.framework.exception.YYException;
import com.yyfax.framework.model.Params;
import com.yyfax.framework.yyfax.enums.RspCodeEnum;
import com.yyfax.pay.common.constants.PayConstants;
import com.yyfax.pay.common.exception.YYPayException;

/**
 * 支付 直连请求参数
 * 
 * @author buyi
 * @date 2017-04-11 13:56:44
 * @since v1.0.0
 */
@SuppressWarnings("serial")
public class PaySendVerifyDirectParams extends Params {
	@NotBlank(message = "用户IP不能为空")
	private String userIp; // 用户端请求IP
	@NotBlank(message = "商户号不能为空")
	private String merchantCode; // 商户编号
	@NotBlank(message = "用户id不能为空")
	private String userId; // 商户用户ID
	@NotBlank(message = "订单号不能为空")
	private String orderId; // 商户订单号
	@NotBlank(message = "订单时间不能为空")
	private String orderTime; // 订单时间
	@NotBlank(message = "过期时间不能为空")
	private String expireTime; // 过期时间
	@NotBlank(message = "终端类型不能为空")
	private String terminal; // 终端
	private String paymentCode; // 偏好支付通道编号
	@NotBlank(message = "异步通知地址不能为空")
	private String notifyUrl; // 异步通知url
	@NotNull(message = "金额不能为空")
	@Min(value = 1, message = "金额必须大于等于1")
	private Integer amount; // 金额

	private String name; // 姓名
	private String idNo; // 身份证号
	private String cardNo; // 银行卡号
	private String phone; // 手机号

	private String serverIp; // 服务器请求Ip,直接从request中获取

	public String getUserIp() {
		return userIp;
	}

	public void setUserIp(String userIp) {
		this.userIp = userIp;
	}

	public String getMerchantCode() {
		return merchantCode;
	}

	public void setMerchantCode(String merchantCode) {
		this.merchantCode = merchantCode;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public String getOrderTime() {
		return orderTime;
	}

	public void setOrderTime(String orderTime) {
		this.orderTime = orderTime;
	}

	public String getExpireTime() {
		return expireTime;
	}

	public void setExpireTime(String expireTime) {
		this.expireTime = expireTime;
	}

	public String getTerminal() {
		return terminal;
	}

	public void setTerminal(String terminal) {
		this.terminal = terminal;
	}

	public String getPaymentCode() {
		return paymentCode;
	}

	public void setPaymentCode(String paymentCode) {
		this.paymentCode = paymentCode;
	}

	public String getNotifyUrl() {
		return notifyUrl;
	}

	public void setNotifyUrl(String notifyUrl) {
		this.notifyUrl = notifyUrl;
	}

	public Integer getAmount() {
		return amount;
	}

	public void setAmount(Integer amount) {
		this.amount = amount;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getIdNo() {
		return idNo;
	}

	public void setIdNo(String idNo) {
		this.idNo = idNo;
	}

	public String getCardNo() {
		return cardNo;
	}

	public void setCardNo(String cardNo) {
		this.cardNo = cardNo;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getServerIp() {
		return serverIp;
	}

	public void setServerIp(String serverIp) {
		this.serverIp = serverIp;
	}

	@Override
	public boolean validate() throws Exception {

		if (StringUtils.isNotBlank(this.orderTime)) {// 如果订单时间不为空，则检验时间格式
			if (!DateUtil.vaildFormat(orderTime, DateUtil.FORMAT_DATETIME)) {
				throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'orderTime' 时间格式不正确" + DateUtil.FORMAT_DATETIME, "订单时间格式错误");
			}

		}

		if (StringUtils.isNotBlank(this.expireTime)) { // 如果过期时间不为空，则检验时间格式
			if (!DateUtil.vaildFormat(expireTime, DateUtil.FORMAT_DATETIME)) {
				throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'expireTime' 时间格式不正确" + DateUtil.FORMAT_DATETIME, "失效时间格式错误");
			}
		}

		// if (StringUtils.isBlank(this.terminal) && !TerminalEnum.IOS.isValue(this.terminal) && !TerminalEnum.ANDROID.isValue(this.terminal)) {
		// throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'terminal'不能为空，或者不支持的终端类型");
		// }

		if (StringUtils.isNotBlank(this.cardNo) || StringUtils.isNotBlank(this.idNo) || StringUtils.isNotBlank(this.name)) {
			// 如果银行卡号、身份证号、或者姓名，存在不为空，则银行卡号、身份证号、姓名都要传值
			// if (StringUtils.isBlank(this.cardNo) || StringUtils.isBlank(this.idNo) || StringUtils.isBlank(this.name)) {
			// throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'cardNo/idNo/name' 存在空值", "证件号码、姓名、银行卡号不能为空");
			// } else {
			// // 身份证号
			// if (!IdCardValidateUtil.isIdCard(idNo)) {
			// throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'idNo' 非法", "证件号码非法");
			// }
			//
			// if (this.name.length() > PayConstants.NAME_MAX_LENGTH) {
			// throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "name非法", "姓名不能超过最大长度" + PayConstants.NAME_MAX_LENGTH);
			// }
			//
			// // 校验姓名
			// if (!ValidateUtil.isName(this.name)) {
			// throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'name' 非法", "姓名非法，只支持中文姓名");
			// }
			//
			// }
			validateIdNoAndNameAndCardNo();
		}

		return true;
	}

	/**
	 * 校验证件号码、姓名、银行卡号
	 * 
	 * @author buyi
	 * @date 2017-05-24 14:13:16
	 * @since v1.0.0
	 * @param idNo
	 * @param name
	 * @param cardNo
	 */
	public void validateIdNoAndNameAndCardNo() {
		if (StringUtils.isBlank(this.cardNo) || StringUtils.isBlank(this.idNo) || StringUtils.isBlank(this.name)) {
			throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'cardNo/idNo/name'不能为空", "证件号码、姓名、银行卡号不能为空");
		} else {
			// 身份证号
			if (!IdCardValidateUtil.isIdCard(idNo)) {
				throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'idNo' 非法", "证件号码非法");
			}

			if (this.name.length() > PayConstants.NAME_MAX_LENGTH) {
				throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "name非法", "姓名不能超过最大长度" + PayConstants.NAME_MAX_LENGTH);
			}

			// 校验姓名
			if (!ValidateUtil.isName(this.name)) {
				throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'name' 非法", "姓名非法，只支持中文姓名");
			}
		}
	}

	/**
	 * 首次支付，三要素校验
	 * 
	 * @author buyi
	 * @date 2017-04-27 10:09:05
	 * @since v1.0.0
	 * @return
	 * @throws YYException
	 */
	public void validateFirst() throws YYException {
		// if (StringUtils.isBlank(this.cardNo) || StringUtils.isBlank(this.idNo) || StringUtils.isBlank(this.name)) {
		// throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'cardNo/idNo/name'不能为空", "证件号码、姓名、银行卡号不能为空");
		// } else {
		// // 身份证号
		// if (!IdCardValidateUtil.isIdCard(idNo)) {
		// throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'idNo' 非法", "证件号码非法");
		// }
		//
		// if (this.name.length() > PayConstants.NAME_MAX_LENGTH) {
		// throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "name非法", "姓名不能超过最大长度" + PayConstants.NAME_MAX_LENGTH);
		// }
		//
		// // 校验姓名
		// if (!ValidateUtil.isName(this.name)) {
		// throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'name' 非法", "姓名非法，只支持中文姓名");
		// }
		// }
		validateIdNoAndNameAndCardNo();

		if (StringUtils.isBlank(this.phone)) {
			throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "'phone'不能为空", "手机号码不能为空");
		}else{
			//验证手机号码合法性
			if(!ValidateUtil.isMobile(this.phone)){
				throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "phone 非法", "手机号码非法");
			}
		}
	}

}
